import {
  globalUrls
} from 'urls.js'
// 优化请求,需求请求所有的数据,在list页面渲染,在首页显示7条
const network = {
  getMovieList: function(params) {
    // let count = params.count ? params.count : 7;
    // wx.request({ //电影请求
    //   url: `${globalUrls.movieList}`,
    //   data: {
    //     count: count
    //   },
    //   success(res) {
    //     let movies = res.data.subject_collection_items;
    //     // 如果有值传过来,并且成功,就把结果返回
    //     if (params && params.success) {
    //       params.success(movies)
    //     }
    //   },
    //   fail(err) {
    //     if (params && params.err) params.err(err)
    //   }
    // });
    params.type = "movies";
    this.getItemList(params)
  },
  getTvList: function(params) {
    params.type = "tv";
    this.getItemList(params)
  },
  getShowList(params) {
    params.type = "show";
    this.getItemList(params)

  },
  // 优化请求,判断传入的是哪种类型,然对赋对应的请求,这种就不能分别写三次相同的代码 
  getItemList(params) {
    let url = '';
    wx.showLoading({
      title: '加载中...',
    })
    if (params.type == "movies") {
      url = globalUrls.movieList;
    } else if (params.type == "tv") {
      url = globalUrls.tvList;
    } else {
      url = globalUrls.showList;
    }
    let count = params.count ? params.count : 7;
    wx.request({
      url: url,
      data: {
        count: count
      },
      success(res) {
        let items = res.data.subject_collection_items;
        // 解决页面只有两条数据,出现左右显示的情况,
        let itemsCount = items.length;
        let left = itemsCount % 3;
        if (left === 2) {
          items.push(null)
        }
        if (params && params.success) params.success(items)
        wx.hideLoading()
      }
    });
  },

  // 页面详情接口,接受页面传入的两个参数type,id
  getItemDetail(params) {
    let type = params.type;
    let id = params.id;
    let url = "";
    if (type === "movies") {
      url = globalUrls.movieDetail + id;
    } else if (type === "tv") {
      url = globalUrls.tvDetail + id;
    } else {
      url = globalUrls.showDetail + id;
    }
    wx.request({
      url: url,
      success(res) {
        let item = res.data;
        if (params && params.success) {
          params.success(item)
        }
      }
    })
  },
  // 详情页面标签接口
  getItemTags(params) {
    let type = params.type;
    let id = params.id;
    let url = '';
    if (type === "movies") {
      url = globalUrls.movieTags(id);
    } else if (type === "tv") {
      url = globalUrls.tvTags(id);
    } else {
      url = globalUrls.showTags(id);
    }
    wx.request({
      url,
      success(res) {
        if (params && params.success) {
          params.success(res)
        }
      }
    })
  },
  // 更多短评的接口
  movieComments(params) {
    wx.showLoading({
      title: '加载中...',
    })
    let type = params.type;
    let id = params.id;
    let start = params.start ? params.start : 0;
    let count = params.count ? params.count : 3;
    let url = '';
    if (type === "movies") {
      url = globalUrls.movieComments(id, start, count);
    } else if (type === "tv") {
      url = globalUrls.tvComments(id, start, count);
    } else {
      url = globalUrls.showComments(id, start, count);
    }
    wx.request({
      url,
      success(res) {
        if (params && params.success) {
          wx.hideLoading()
          params.success(res)
        }
      }
    })

  },
  getSerachItem(params){
    let q = params.value;
    let url = globalUrls.searchUrl(q);
    wx.request({
      url: url,
      success:function(res){
        let data = res.data.subjects;
        if (params && params.success){
          params.success(data)
        }
      }
    })
  }

}
export {
  network
};